﻿using System;
using System.Collections;
using System.Data;
namespace GeneralLibs.Lists
{
    public class CauHoi_DA : DoiTuong
    {
        #region LoaiPhieu property
        public string LoaiPhieu
        {
            get
            {
                if (_DataRow["LoaiPhieu"] is string) return (string)_DataRow["LoaiPhieu"];
                return "";
            }
            set
            {
                _DataRow["LoaiPhieu"] = value;
            }
        }
        #endregion

        #region NoiDungHoi property
        public string NoiDungHoi
        {
            get
            {
                if (_DataRow["NoiDungHoi"] is string) return (string)_DataRow["NoiDungHoi"];
                return "";
            }
            set
            {
                _DataRow["NoiDungHoi"] = value;
            }
        }
        #endregion

        #region MaCau property
        public int MaCau
        {
            get
            {
                if ((int)_DataRow["MaCau"] is int) return (int)_DataRow["MaCau"];
                else return 0;
            }
            set
            {
                _DataRow["MaCau"] = value;
            }
        }
        #endregion

        #region LevelTraLoi property
        public int LevelTraLoi
        {
            get
            {
                if (_DataRow["LevelTraLoi"] is int) return (int)_DataRow["LevelTraLoi"];
                else return -1;
            }
            set
            {
                _DataRow["LevelTraLoi"] = value;
            }
        }
        #endregion

        #region bool CoTraLoiKhac property
        public bool? CoTraLoiKhac
        {
            get
            {
                if ((bool)_DataRow["CoTraLoiKhac"] is bool) return (bool)_DataRow["CoTraLoiKhac"];
                else return null;
            }
            set
            {
                _DataRow["CoTraLoiKhac"] = value;
            }
        }
        #endregion

        #region bool? ChonNhieuTraLoi property
        public bool? ChonNhieuTraLoi
        {
            get
            {
                if ((bool)_DataRow["ChonNhieuTraLoi"] is bool) return (bool)_DataRow["ChonNhieuTraLoi"];
                else return null;
            }
            set
            {
                _DataRow["ChonNhieuTraLoi"] = value;
            }
        }
        #endregion

        #region TraLoi1 property
        public string TraLoi1
        {
            get
            {
                if (_DataRow["TraLoi1"] is string) return (string)_DataRow["TraLoi1"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi1"] = value;
            }
        }
        #endregion

        #region TraLoi2 property
        public string TraLoi2
        {
            get
            {
                if (_DataRow["TraLoi2"] is string) return (string)_DataRow["TraLoi2"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi2"] = value;
            }
        }
        #endregion

        #region TraLoi3 property
        public string TraLoi3
        {
            get
            {
                if (_DataRow["TraLoi3"] is string) return (string)_DataRow["TraLoi3"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi3"] = value;
            }
        }
        #endregion

        #region TraLoi4 property
        public string TraLoi4
        {
            get
            {
                if (_DataRow["TraLoi4"] is string) return (string)_DataRow["TraLoi4"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi4"] = value;
            }
        }
        #endregion

        #region TraLoi5 property
        public string TraLoi5
        {
            get
            {
                if (_DataRow["TraLoi5"] is string) return (string)_DataRow["TraLoi5"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi5"] = value;
            }
        }
        #endregion

        #region TraLoi6 property
        public string TraLoi6
        {
            get
            {
                if (_DataRow["TraLoi6"] is string) return (string)_DataRow["TraLoi6"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi6"] = value;
            }
        }
        #endregion

        #region TraLoi7 property
        public string TraLoi7
        {
            get
            {
                if (_DataRow["TraLoi7"] is string) return (string)_DataRow["TraLoi7"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi7"] = value;
            }
        }
        #endregion

        #region TraLoi8 property
        public string TraLoi8
        {
            get
            {
                if (_DataRow["TraLoi8"] is string) return (string)_DataRow["TraLoi8"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi8"] = value;
            }
        }
        #endregion

        #region TraLoi9 property
        public string TraLoi9
        {
            get
            {
                if (_DataRow["TraLoi9"] is string) return (string)_DataRow["TraLoi9"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi9"] = value;
            }
        }
        #endregion

        #region TraLoi10 property
        public string TraLoi10
        {
            get
            {
                if (_DataRow["TraLoi10"] is string) return (string)_DataRow["TraLoi10"];
                else return "";
            }
            set
            {
                _DataRow["TraLoi10"] = value;
            }
        }
        #endregion

        public CauHoi_DA(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucCauHoi_DA : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucCauHoi_DA _Instance;
        public static DanhMucCauHoi_DA Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucCauHoi_DA();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "CauHoi_DA";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new CauHoi_DA((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public CauHoi_DA this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new CauHoi_DA(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public CauHoi_DA this[int indexView, bool a]
        {
            get
            {
                if (0 <= indexView && indexView <= this.Count)
                {
                    return new CauHoi_DA(DataView.ToTable().Rows[indexView]);
                }
                else
                {
                    return null;
                }
            }
        }

        public CauHoi_DA NewDoiTuong()
        {
            return new CauHoi_DA(DataTable.NewRow());
        }

        public CauHoi_DA GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new CauHoi_DA(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public CauHoi_DA GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucCauHoi_DA GetAll()
        {
            return Instance;
        }

        public static DanhMucCauHoi_DA Filter(string filterExpression)
        {
            try
            {
                DanhMucCauHoi_DA obj = new DanhMucCauHoi_DA();
                obj.DataView = new DataView(DanhMucCauHoi_DA.Instance.DataTable, filterExpression, "LoaiPhieu", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static CauHoi_DA GetByMa(string ma)
        {
            try
            {
                return DanhMucCauHoi_DA.Instance.GetByProperty("Ma", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static CauHoi_DA GetByLoaiPhieu_MaCau(string loaiPhieu, int maCau)
        {
            try
            {
                DanhMucCauHoi_DA dm = Filter(string.Format("LoaiPhieu = '{0}' AND MaCau = {1}", loaiPhieu, maCau));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static CauHoi_DA GetByLoaiPhieu_MaCau(string loaiPhieu, int maCau, int levelTraLoi)
        {
            try
            {
                DanhMucCauHoi_DA dm = Filter(string.Format("LoaiPhieu = '{0}' AND MaCau = {1} AND LevelTraLoi = {2}", loaiPhieu, maCau, levelTraLoi));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
